home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_171 / sozobon-c / libs / gifts / gifts.zoo / ssed.c < prev   
C/C++ Source or Header  |  1988-11-09  |  4KB  |  207 lines

  1. /*
  2.  * SSED        substitution stream editor (a very small subset of SED)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <malloc.h>
  8. #include <ctype.h>
  9. #include <errno.h>
  10.  
  11. char        lnbuf[512];        /* line buffer */
  12. char        **old = NULL;        /* old string list (search for) */
  13. char        **new = NULL;        /* new string list (replace with) */
  14. int        s_flag = FALSE;        /* modify files "in place" */
  15.  
  16. typedef    char    **LIST;
  17.  
  18. int lstlen(list)
  19.     register LIST list;
  20.     {
  21.     register int len = 0;
  22.  
  23.     if(list == NULL)
  24.         return(0);
  25.     while(*list++)
  26.         ++len;
  27.     return(len);
  28.     }
  29.  
  30. LIST lstcat(list, item)
  31.     register LIST list;
  32.     register char *item;
  33.     {
  34.     register int len;
  35.  
  36.     len = lstlen(list);
  37.     list = (LIST) realloc(list, (len + 2) * sizeof(char *));
  38.     if(list == NULL)
  39.         return(NULL);
  40.     list[len] = item;
  41.     list[len+1] = NULL;
  42.     return(list);
  43.     }
  44.  
  45. version()
  46.     {
  47.     printf("ssed v1.2 -- 10/19/88 by Dale Schumacher\n");
  48.     exit(1);
  49.     }
  50.  
  51. usage()
  52.     {
  53.     fprintf(stderr,
  54. "usage: ssed [-s] [-e command] [-f commandfile] [infile...] >outfile\n");
  55.     fprintf(stderr,
  56. "    -s    files are modified in place (standalone).\n");
  57.     fprintf(stderr,
  58. "    -e    s/oldtext/newtext/ command on the command line.\n");
  59.     fprintf(stderr,
  60. "    -f    s/oldtext/newtext/ commands read from a file.\n");
  61.     exit(EXIT_FAILURE);
  62.     }
  63.  
  64. main(argc, argv)
  65.     int argc;
  66.     char *argv[];
  67.     {
  68.     FILE *fp, *ftmp;        /* working and temporary files */
  69.     char *fname;            /* working file name */
  70.     char tname[128];        /* temporary file name */
  71.     int c;
  72.     extern int optind;
  73.     extern char *optarg;
  74.  
  75.     while((c = getopt(argc, argv, "se:f:V")) != EOF)
  76.         {
  77.         switch(c)
  78.             {
  79.             case 's':
  80.                 s_flag = TRUE;
  81.                 break;
  82.             case 'e':
  83.                 parse(optarg);
  84.                 break;
  85.             case 'f':
  86.                 if((fp = fopen(optarg, "r")) == NULL)
  87.                     {
  88.                     perrorf("can't open command file '%s'",
  89.                         optarg);
  90.                     exit(EXIT_FAILURE);
  91.                     }
  92.                 errno = 0;
  93.                 while(fgets(lnbuf, sizeof(lnbuf), fp))
  94.                     parse(lnbuf);
  95.                 fclose(fp);
  96.                 if(errno)
  97.                     perror(optarg);
  98.                 break;
  99.             case 'V':
  100.                 version();
  101.             case '?':
  102.                 usage();
  103.             }
  104.         }
  105.     if(old == NULL)            /* no commands specified */
  106.         usage;
  107.     if(optind >= argc)        /* no files specified */
  108.         {
  109.         if(isatty(stdin) || s_flag)
  110.             usage();
  111.         else
  112.             ssed(stdin, stdout);
  113.         }
  114.     else
  115.         {
  116.         for(; optind < argc; ++optind)
  117.             {
  118.             fname = argv[optind];
  119.             if(!strcmp(fname, "-"))
  120.                 fp = stdin;
  121.             else if((fp = fopen(fname, "r")) == NULL)
  122.                 {
  123.                 perrorf("can't open file '%s'", fname);
  124.                 exit(EXIT_FAILURE);
  125.                 }
  126.             if(s_flag)
  127.                 {
  128.                 tmpnam(tname);
  129.                 if((ftmp = fopen(tname, "w")) == NULL)
  130.                     {
  131.                     perrorf("can't open temp file '%s'",
  132.                         tname);
  133.                     exit(EXIT_FAILURE);
  134.                     }
  135.                 }
  136.             else
  137.                 ftmp = stdout;
  138.             errno = 0;
  139.             ssed(fp, ftmp);
  140.             fclose(fp);
  141.             if(s_flag)
  142.                 {
  143.                 fclose(ftmp);
  144.                 unlink(fname);
  145.                 if(rename(tname, fname) != 0)
  146.                     {
  147.                     perrorf("can't rename '%s' to '%s'.\n",
  148.                         tname, fname);
  149.                     exit(EXIT_FAILURE);
  150.                     }
  151.                 }
  152.             if(errno)
  153.                 perror(NULL);
  154.             }
  155.         }
  156.     exit(EXIT_SUCCESS);
  157.     }
  158.  
  159. parse(cmd)
  160.     register char *cmd;
  161.     {
  162.     register char *p, *q;
  163.     register char delim;
  164.     char buf[512];
  165.  
  166.     if(!(cmd && *cmd))
  167.         return;
  168.     cmd = strcpy(buf, cmd);
  169.     if(tolower(*cmd) != 's')
  170.         {
  171.         fprintf(stderr,
  172.         "ssed: only 's/oldtext/newtext/' commands are implemented.\n");
  173.         exit(EXIT_FAILURE);
  174.         }
  175.     if(((delim = cmd[1]) == '\0')
  176.     || ((q = strchr((cmd + 2), delim)) == NULL)
  177.     || ((p = strchr((q + 1), delim)) == NULL))
  178.         {
  179.         fprintf(stderr, "ssed: bad command syntax '%s'\n", cmd);
  180.         exit(EXIT_FAILURE);
  181.         }
  182.     *p = '\0';
  183.     *q++ = '\0';
  184.     p = cmd + 2;
  185.     if(((old = lstcat(old, strdup(p))) == NULL)
  186.     || ((new = lstcat(new, strdup(q))) == NULL))
  187.         {
  188.         fprintf(stderr, "ssed: out of memory.\n");
  189.         exit(EXIT_FAILURE);
  190.         }
  191.     }
  192.  
  193. ssed(fin, fout)
  194.     FILE *fin, *fout;
  195.     {
  196.     register char **pp, **qq;
  197.  
  198.     while(fgets(lnbuf, sizeof(lnbuf), fin))
  199.         {
  200.         for(pp = old, qq = new; *pp && *qq; ++pp, ++qq)
  201.             {
  202.             strrpl(lnbuf, *pp, *qq, -1);
  203.             }
  204.         fputs(lnbuf, fout);
  205.         }
  206.     }
  207.